<script setup lang="ts">
import { ref, computed, h } from 'vue';
import { Menu } from 'ant-design-vue';
import { IconifyIcon } from '@vben/icons';

// Props
interface Props {
  treeData: any[];
  selectedResourceId?: string | null;
  resourceTypeConfig: Record<string, any>;
  collapsed?: boolean;
}

const props = withDefaults(defineProps<Props>(), {
  selectedResourceId: null,
});

// Emits
const emit = defineEmits<{
  'update:selectedResourceId': [id: string];
  createResource: [];
  createSubResource: [parentId: string];
  editResource: [id: string];
  deleteResource: [id: string];
}>();

// 计算属性
const selectedKeys = computed(() => {
  return props.selectedResourceId
    ? [`resource-${props.selectedResourceId}`]
    : [];
});

// 事件处理
const handleSelect = (info: any) => {
  const resourceId = String(info.key).replace('resource-', '');
  emit('update:selectedResourceId', resourceId);
};

// 转换树数据为菜单数据
const convertToMenuData = (nodes: any[]): any[] => {
  return nodes.map((node) => ({
    key: node.key,
    label: node.title,
    icon: () =>
      h(IconifyIcon, {
        icon: node.icon || node.data?.icon || 'ant-design:file-outlined',
        style: { color: node.color || node.data?.color || '#666' },
      }),
    children: node.children?.length
      ? convertToMenuData(node.children)
      : undefined,
  }));
};

const menuData = computed(() => convertToMenuData(props.treeData));
</script>

<template>
  <div class="resource-menu-container">
    <div class="menu-content" v-show="!props.collapsed">
      <Menu
        :selected-keys="selectedKeys"
        :items="menuData"
        mode="inline"
        @select="handleSelect"
        class="custom-menu"
      />
    </div>
  </div>
</template>

<style lang="less" scoped>
.resource-menu-container {
  height: 100%;
  display: flex;
  flex-direction: column;
  background: white;

  .menu-content {
    flex: 1;
    overflow-y: auto;
    padding: 8px 0;
    background: #fff;

    :deep(.ant-menu) {
      background: transparent;
      border: none;

      .ant-menu-item {
        position: relative;
        margin: 2px 8px;
        border-radius: 8px;
        transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        height: auto;
        line-height: 1;
        padding: 0;

        &:hover {
          background: rgba(255, 255, 255, 0.95);
          border-color: rgba(102, 126, 234, 0.2);
          transform: translateY(-1px);
          box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
        }

        &.ant-menu-item-selected {
          background: linear-gradient(
            135deg,
            rgba(102, 126, 234, 0.1) 0%,
            rgba(118, 75, 162, 0.1) 100%
          );
          box-shadow:
            0 0 0 2px rgba(102, 126, 234, 0.3),
            0 4px 16px rgba(102, 126, 234, 0.2);
          transform: translateY(-2px);
        }

        .ant-menu-title-content {
          padding: 12px 16px;
          min-height: 48px;
          display: flex;
          align-items: center;
        }
      }

      .ant-menu-submenu {
        .ant-menu-submenu-title {
          margin: 2px 8px;
          border-radius: 8px;
          transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
          height: auto;
          line-height: 1;
          padding: 0;

          &:hover {
            background: rgba(255, 255, 255, 0.95);
            border-color: rgba(102, 126, 234, 0.2);
            transform: translateY(-1px);
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
          }

          .ant-menu-title-content {
            padding: 12px 16px;
            min-height: 48px;
            display: flex;
            align-items: center;
          }
        }
      }
    }

    .menu-node {
      display: flex;
      width: 100%;
      cursor: pointer;

      .node-content {
        display: flex;
        align-items: center;
        justify-content: space-between;
        width: 100%;

        .node-info {
          display: flex;
          align-items: center;
          gap: 8px;
          flex: 1;

          .node_icon {
            display: flex;
            align-items: center;
            justify-content: center;
            width: 32px;
            height: 32px;
            border-radius: 8px;
            background: linear-gradient(
              135deg,
              rgba(102, 126, 234, 0.1) 0%,
              rgba(118, 75, 162, 0.1) 100%
            );
            flex-shrink: 0;
            position: relative;
            overflow: hidden;

            &::before {
              content: '';
              position: absolute;
              top: 0;
              left: 0;
              right: 0;
              bottom: 0;
              background: linear-gradient(
                135deg,
                rgba(255, 255, 255, 0.2) 0%,
                transparent 100%
              );
              border-radius: 8px;
            }

            .node-icon {
              font-size: 16px;
              position: relative;
              z-index: 1;
              filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.1));
            }
          }

          .node-title {
            font-size: 15px;
            font-weight: 600;
            color: #2c3e50;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            min-width: 100px;
            line-height: 1.4;
            margin: 0;
            background: transparent;
            text-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
            transition: color 0.3s ease;
          }

          .node-count {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            min-width: 20px;
            height: 20px;
            padding: 0 8px;
            font-size: 11px;
            font-weight: 600;
            color: #667eea;
            background: linear-gradient(
              135deg,
              rgba(102, 126, 234, 0.1) 0%,
              rgba(118, 75, 162, 0.1) 100%
            );
            border: 1px solid rgba(102, 126, 234, 0.2);
            border-radius: 10px;
            line-height: 1;
            backdrop-filter: blur(10px);
            box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
          }

          .expand-hint {
            display: inline-flex;
            align-items: center;
            margin-left: 8px;

            .expand-icon {
              font-size: 12px;
              color: #8c8c8c;
              transition: all 0.3s ease;
            }
          }
        }

        .node-actions {
          position: absolute;
          right: -60px;
          top: 50%;
          z-index: 9999;
          transform: translateY(-50%);
          display: flex;
          align-items: center;
          gap: 4px;
          background: rgba(255, 255, 255, 0.95);
          border: 1px solid rgba(0, 0, 0, 0.1);
          border-radius: 8px;
          padding: 4px;
          box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
          backdrop-filter: blur(10px);

          .action-btn {
            width: 24px;
            height: 24px;
            padding: 0;
            border: none;
            background: transparent;
            color: #8c8c8c;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 6px;
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);

            .anticon {
              font-size: 12px;
            }

            &:hover {
              color: #667eea;
              background: linear-gradient(
                135deg,
                rgba(102, 126, 234, 0.1) 0%,
                rgba(118, 75, 162, 0.1) 100%
              );
              transform: scale(1.1);
            }

            &.delete-btn:hover {
              color: #ff6b6b;
              background: linear-gradient(
                135deg,
                rgba(255, 107, 107, 0.1) 0%,
                rgba(255, 77, 79, 0.1) 100%
              );
            }
          }
        }
      }
    }
  }
}
</style>
